import pygame
import modTank

class MyGame:
    def __init__(self):
        pygame.init()
        pygame.display.set_caption("移动坦克")
        self._screen = pygame.display.set_mode((800, 600))
        self._timer = pygame.time.Clock()
        self._margin = 8

        # 五辆坦克
        self._tanks = []
        for i in range(5):
            self._tanks.append(modTank.Tank())
    
    def play(self):
        self._set_tanks_pos()

        keep_going = True
        while keep_going:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    keep_going = False
            
            self._screen.fill((0, 0, 0))
            # 坦克
            for tank in self._tanks:
                tank.render(self._screen)
                tank.update()

            pygame.display.update()
            self._timer.tick(60)

        pygame.quit()
    
    def _set_tanks_pos(self):
        tankW = self._tanks[0].width
        tankL = self._tanks[0].length
        # 五辆坦克分散
        tankX = (self._screen.get_width() - tankW) / 2 # 水平居中
        tankY = self._margin # 垂直靠顶部
        tankSpace = (self._screen.get_width() - self._margin*2 - tankW*len(self._tanks)) / 4
        for i in range(len(self._tanks)):
            self._tanks[i].SetPositon((tankW+tankSpace)*i+self._margin, tankY)
    
myGame = MyGame()
myGame.play()